package com.fa.config;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
public class R<T> {

    private int code;          // 状态码
    private String message;     // 提示信息
    private T data;             // 返回的数据
    private PageInfo pageInfo;  // 分页信息

    // 构造函数
    public R() {
    }

    public R(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public R(int code, String message, T data, PageInfo pageInfo) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.pageInfo = pageInfo;
    }

    // 成功时的静态方法
    public static <T> R<T> success(T data) {
        return new R<>(200, "Success", data);
    }

    public static <T> R<T> success(String message, T data) {
        return new R<>(200, message, data);
    }

    public static <T> R<T> success() {
        return new R<>(200, "Success", null);
    }

    public static <T> R<T> success(T data, PageInfo pageInfo) {
        return new R<>(200, "Success", data, pageInfo);
    }

    // 失败时的静态方法
    public static <T> R<T> error(int code, String message) {
        return new R<>(code, message, null);
    }

    public static <T> R<T> error(String message) {
        return new R<>(500, message, null);
    }


    @Override
    public String toString() {
        return "R{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                ", pageInfo=" + pageInfo +
                '}';
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    // 内部分页信息类
    public static class PageInfo {
        private Long currentPage;
        private Long totalPages;
        private Long totalItems;
        private Long pageSize;
        private Long pageNo;

    }
}
